import { deepClone } from '@/utils'
import type { LayoutRouterRaw } from '@/@type'
import { createRouter, createWebHistory } from 'vue-router'

const files: Record<string, any> = import.meta.globEager('../router/*.module.ts')

const layoutRouters: Array<LayoutRouterRaw> = []

for (const key in files) {
  layoutRouters.push(files[key].default)
}

layoutRouters.sort((x, y) => x.sort - y.sort)

const backupLayoutRuoters = deepClone(layoutRouters)

export {
  backupLayoutRuoters
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Layout',
      component: () => import('@/layout/index.vue'),
      children: [...layoutRouters]
    },
    // 静态路由
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue')
    },
    {
      path: '/403',
      name: 'Error403',
      component: () => import('@/views/Error403.vue')
    },
    {
      path: '/404',
      name: 'Error404',
      component: () => import('@/views/Error404.vue')
    },
    {
      path: '/:catchAll(.*)',
      redirect: '/404'
    }
  ]
})

export default router
